package loja.converters;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import loja.model.compra.pedido.PedidoCompra;
import loja.services.factory.ServiceFactory;
import loja.services.factory.helper.ServiceFactoryHelper;

@FacesConverter(forClass = PedidoCompra.class)
public class PedidoCompraConverter implements Converter {

	private ServiceFactory serviceFactory = ServiceFactoryHelper
			.getServiceFactory();

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1,
			String stringPedidoCompra) {

		if (stringPedidoCompra == null)
			return null;

		if (stringPedidoCompra.trim().length() == 0)
			return null;

		Long idPedidoCompra = Long.valueOf(stringPedidoCompra);
		return this.serviceFactory.getPedidoCompraService().consultarPorId(
				idPedidoCompra);

	}

	@Override
	public String getAsString(FacesContext arg0, UIComponent arg1,
			Object objPedidoCompra) {
		if (objPedidoCompra == null)
			return null;
		PedidoCompra pedidoCompra = (PedidoCompra) objPedidoCompra;
		return String.valueOf(pedidoCompra.getIdPedidoCompra());
	}

}
